package com.skywolf.chem.common.sysconfig.utils;

/**
 * Created by pei_pei on 2020/10/27.
 */

import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.IOUtils;
import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import org.lionsoul.ip2region.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

public final class IpUtils {
    private static final Logger log = LoggerFactory.getLogger(IpUtils.class);
    private static final String IP_INFO_URL = "http://ip.taobao.com/service/getIpInfo.php";

    public IpUtils() {
    }

    public static String getIpAddr(HttpServletRequest request) {
        String ipAddress;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }

            if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }

            if(ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if(ipAddress.equals("127.0.0.1")) {
                    InetAddress e = null;

                    try {
                        e = InetAddress.getLocalHost();
                    } catch (UnknownHostException var4) {
                        var4.printStackTrace();
                    }

                    ipAddress = e.getHostAddress();
                }
            }

            if(ipAddress != null && ipAddress.length() > 15 && ipAddress.indexOf(",") > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        } catch (Exception var5) {
            ipAddress = "";
        }

        return ipAddress;
    }

    public static String ip2Region(String ip) {
        try {
            boolean e = Util.isIpAddress(ip);
            if(!e) {
                return "";
            } else {
                DbConfig config = new DbConfig();
                InputStream inputStream = IpUtils.class.getClassLoader().getResourceAsStream("ip2region.db");
                byte[] bytes = IOUtils.toByteArray(inputStream);
                DbSearcher searcher = new DbSearcher(config, bytes);
                DataBlock block = searcher.memorySearch(ip);
                return block.getRegion();
            }
        } catch (Exception var7) {
            log.error("ip解析异常", var7);
            return "";
        }
    }

    public static void main(String[] args) {
        System.out.println(getRegionAddress("118.213.176.78"));
    }

    public static String getRegionAddress(String ip) {
        try {
            String e = call("ip=" + ip);
            if(StringUtils.hasText(e)) {
                IpUtils.IpInfoResponse response = (IpUtils.IpInfoResponse)JSONObject.parseObject(e, IpUtils.IpInfoResponse.class);
                if(response.getCode().intValue() == 0) {
                    return response.getData().getFullAddress();
                }
            }
        } catch (Exception var3) {
            log.error("调用淘宝ip解析异常", var3);
        }

        return "";
    }

    public static String call(String params) {
        HttpURLConnection connection = null;

        try {
            URL url = new URL("http://ip.taobao.com/service/getIpInfo.php");
            connection = (HttpURLConnection)url.openConnection();
            connection.setConnectTimeout(3000);
            connection.setReadTimeout(3000);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.connect();
            DataOutputStream e = new DataOutputStream(connection.getOutputStream());
            e.writeBytes(params);
            e.flush();
            e.close();
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
            StringBuffer buffer = new StringBuffer();
            String line = "";

            while((line = reader.readLine()) != null) {
                buffer.append(line);
            }

            reader.close();
            String var7 = buffer.toString();
            return var7;
        } catch (Exception var11) {
            log.error("调用淘宝ip解析异常", var11);
        } finally {
            connection.disconnect();
        }

        return null;
    }

    public static class IpInfo {
        private String country;
        private String area;
        private String region;
        private String city;

        public String getFullAddress() {
            return this.country + this.area + this.region + this.city;
        }

        public IpInfo() {
        }

        public String getCountry() {
            return this.country;
        }

        public String getArea() {
            return this.area;
        }

        public String getRegion() {
            return this.region;
        }

        public String getCity() {
            return this.city;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        public void setArea(String area) {
            this.area = area;
        }

        public void setRegion(String region) {
            this.region = region;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public boolean equals(Object o) {
            if(o == this) {
                return true;
            } else if(!(o instanceof IpUtils.IpInfo)) {
                return false;
            } else {
                IpUtils.IpInfo other = (IpUtils.IpInfo)o;
                if(!other.canEqual(this)) {
                    return false;
                } else {
                    label59: {
                        String this$country = this.getCountry();
                        String other$country = other.getCountry();
                        if(this$country == null) {
                            if(other$country == null) {
                                break label59;
                            }
                        } else if(this$country.equals(other$country)) {
                            break label59;
                        }

                        return false;
                    }

                    String this$area = this.getArea();
                    String other$area = other.getArea();
                    if(this$area == null) {
                        if(other$area != null) {
                            return false;
                        }
                    } else if(!this$area.equals(other$area)) {
                        return false;
                    }

                    String this$region = this.getRegion();
                    String other$region = other.getRegion();
                    if(this$region == null) {
                        if(other$region != null) {
                            return false;
                        }
                    } else if(!this$region.equals(other$region)) {
                        return false;
                    }

                    String this$city = this.getCity();
                    String other$city = other.getCity();
                    if(this$city == null) {
                        if(other$city != null) {
                            return false;
                        }
                    } else if(!this$city.equals(other$city)) {
                        return false;
                    }

                    return true;
                }
            }
        }

        protected boolean canEqual(Object other) {
            return other instanceof IpUtils.IpInfo;
        }

        public int hashCode() {
            boolean PRIME = true;
            byte result = 1;
            String $country = this.getCountry();
            int result1 = result * 59 + ($country == null?43:$country.hashCode());
            String $area = this.getArea();
            result1 = result1 * 59 + ($area == null?43:$area.hashCode());
            String $region = this.getRegion();
            result1 = result1 * 59 + ($region == null?43:$region.hashCode());
            String $city = this.getCity();
            result1 = result1 * 59 + ($city == null?43:$city.hashCode());
            return result1;
        }

        public String toString() {
            return "IpUtils.IpInfo(country=" + this.getCountry() + ", area=" + this.getArea() + ", region=" + this.getRegion() + ", city=" + this.getCity() + ")";
        }
    }

    public static class IpInfoResponse {
        private Integer code;
        private IpUtils.IpInfo data;

        public IpInfoResponse() {
        }

        public Integer getCode() {
            return this.code;
        }

        public IpUtils.IpInfo getData() {
            return this.data;
        }

        public void setCode(Integer code) {
            this.code = code;
        }

        public void setData(IpUtils.IpInfo data) {
            this.data = data;
        }

        public boolean equals(Object o) {
            if(o == this) {
                return true;
            } else if(!(o instanceof IpUtils.IpInfoResponse)) {
                return false;
            } else {
                IpUtils.IpInfoResponse other = (IpUtils.IpInfoResponse)o;
                if(!other.canEqual(this)) {
                    return false;
                } else {
                    Integer this$code = this.getCode();
                    Integer other$code = other.getCode();
                    if(this$code == null) {
                        if(other$code != null) {
                            return false;
                        }
                    } else if(!this$code.equals(other$code)) {
                        return false;
                    }

                    IpUtils.IpInfo this$data = this.getData();
                    IpUtils.IpInfo other$data = other.getData();
                    if(this$data == null) {
                        if(other$data != null) {
                            return false;
                        }
                    } else if(!this$data.equals(other$data)) {
                        return false;
                    }

                    return true;
                }
            }
        }

        protected boolean canEqual(Object other) {
            return other instanceof IpUtils.IpInfoResponse;
        }

        public int hashCode() {
            boolean PRIME = true;
            byte result = 1;
            Integer $code = this.getCode();
            int result1 = result * 59 + ($code == null?43:$code.hashCode());
            IpUtils.IpInfo $data = this.getData();
            result1 = result1 * 59 + ($data == null?43:$data.hashCode());
            return result1;
        }

        public String toString() {
            return "IpUtils.IpInfoResponse(code=" + this.getCode() + ", data=" + this.getData() + ")";
        }
    }
}
